import {
	getToken,
	setToken
} from '@/utils/auth'
import {
	logout,
	login,
	smsLogin,
	maPhoneLogin,
	pwdLogin
} from '@/api/login.js'
import {
	getUserInfo
} from '@/api/userinfo.js'

const user = {
	state: {
		token: getToken(),
		userInfo: uni.getStorageSync('user_info')
	},

	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token
		},
		SET_USER_INFO: (state, userInfo) => {
			state.userInfo = userInfo
			uni.setStorageSync('user_info', userInfo);
		},
	},
	actions: {
		BeforeLogin({
			commit
		}) {
			return new Promise((resolve, reject) => {
				uni.login({
					//#ifdef MP-WEIXIN
					provider: 'weixin', //使用微信登录
					//#endif
					//#ifdef MP-ALIPAY
					provider: 'alipay', //使用支付宝登录
					//#endif
					//#ifdef MP-TOUTIAO
					provider: 'alipay', //使用抖音登录
					//#endif
					success: function(loginRes) {
						console.log("code" + loginRes.code);
						login({
							jsCode: loginRes.code
						}).then(response => {
							let userInfo = response.data;
							setToken(userInfo.hxToken)
							commit('SET_TOKEN', userInfo.hxToken)
							commit('SET_USER_INFO', userInfo)
							resolve("success");
						}).catch(error => {
							reject(error)
						})
					}
				});
			});
		},
		//微信小程序登录
		LoginWxMa({
			commit
		}) {
			return new Promise((resolve, reject) => {
				uni.login({
					success: function(res) {
						if (res.code) {
							let wxUserDTO = {
								jsCode: res.code,
								loginType: "WECHAT_LOGIN"
							};
							login(wxUserDTO).then(res => {
								let userInfo = res.data;
								setToken(userInfo.hxToken)
								commit('SET_TOKEN', userInfo.hxToken)
								commit('SET_USER_INFO', userInfo)
								resolve("success");
							}).catch(error => {
								reject(error)
							})
						}
					}
				});
			});
		},
		//支付宝小程序登录
		LoginAliMa({
			commit
		}) {
			return new Promise((resolve, reject) => {
				uni.login({
					provider: 'ali',
					scopes: 'auth_base',
					success: function(res) {
						if (res.code) {
							let aliUserDTO = {
								jsCode: res.code,
								loginType: "ALIPAY_LOGIN"
							};
							login(aliUserDTO).then(res => {
								let userInfo = res.data;
								setToken(userInfo.hxToken)
								commit('SET_TOKEN', userInfo.hxToken)
								commit('SET_USER_INFO', userInfo)
								resolve("success");
							}).catch(error => {
								reject(error)
							})
						}
					}
				});
			});
		},
		// 获取用户信息
		GetInfo({
			commit
		}) {
			return new Promise((resolve, reject) => {
				getUserInfo().then(res => {
					commit('SET_USER_INFO', res.data);
					resolve(res.data)
				}).catch(error => {
					reject(error)
				})
			})
		},
		// 短信登录
		SmsLogin({
			commit
		}, info) {
			return new Promise((resolve, reject) => {
				smsLogin(info).then(res => {
					let userInfo = res.data;
					setToken(userInfo.hxToken)
					commit('SET_TOKEN', userInfo.hxToken)
					commit('SET_USER_INFO', userInfo)
					resolve(userInfo);
				}).catch(error => {
					reject(error)
				})
			});
		},
		// 小程序手机号登录
		MaPhoneLogin({
				commit
			},
			info) {
			return new Promise((resolve, reject) => {
				maPhoneLogin(info).then(res => {
					let userInfo = res.data;
					setToken(userInfo.hxToken)
					commit('SET_TOKEN', userInfo.hxToken)
					commit('SET_USER_INFO', userInfo)
					resolve(userInfo);
				}).catch(error => {
					reject(error)
				})
			});

		},
		// 用户密码登录
		UserPwdLogin({
				commit
			},
			info) {
			return new Promise((resolve, reject) => {
				pwdLogin(info).then(res => {
					let userInfo = res.data;
					setToken(userInfo.hxToken)
					commit('SET_TOKEN', userInfo.hxToken)
					commit('SET_USER_INFO', userInfo)
					resolve(userInfo);
				}).catch(error => {
					reject(error)
				})
			});

		},

		// 退出
		Logout({
			commit
		}) {
			return new Promise((resolve, reject) => {
				logout({}).then(res => {
					let userInfo = res.data;
					commit('SET_TOKEN', userInfo.hxToken)
					commit('SET_USER_INFO', userInfo)

					resolve();
				}).catch(error => {
					reject(error)
				})

			});
		}


	},
}
export default user
